package com.dartfar.workflow.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dartfar.base.dao.DaoUtil;
import com.dartfar.base.model.BaseEntity;
import com.dartfar.base.service.BaseServiceImpl;
import com.dartfar.util.EntityUtils;
import com.dartfar.util.SpringContextUtil;
import com.dartfar.util.StringUtils;
import com.dartfar.workflow.model.WorkflowEvents;

@Component("workflowEventsService")
public class WorkflowEventsServiceImpl extends BaseServiceImpl<WorkflowEvents> implements WorkflowEventsService {

	static Logger logger = Logger.getLogger(WorkflowEventsServiceImpl.class);
	
	@Resource
	private DaoUtil<WorkflowEvents> workflowEventsDaoUtil;
	
	/**
	 * 根据流程定义ID获取流程事件分页列表
	 * @param id
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<WorkflowEvents> getListForPageByWorkflowProcessId(int id,
			int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		String hql = "from WorkflowEvents w where w.workflowProcessFunMount.id = " + id + " order by w.orderIndex asc";
		return workflowEventsDaoUtil.getListForPage(hql, (pageNo-1)*pageSize, pageSize);
	}
	
	/**
	 * 根据流程定义ID获取流程事件总数
	 * @param id
	 * @return
	 */
	@Override
	public Long getCountByWorkflowProcessId(int id){
		String hql = "from WorkflowEvents w where w.workflowProcessFunMount.id = " + id;
		return workflowEventsDaoUtil.getCount(hql);
	}
	
	
	
	/**
	 * 根据任务挂接ID获取流程事件分页列表
	 * @param id
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<WorkflowEvents> getListForPageByWorkflowTaskId(int id,
			int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		String hql = "from WorkflowEvents w where w.workflowTaskFunMount.id = " + id + " order by w.orderIndex asc";
		return workflowEventsDaoUtil.getListForPage(hql, (pageNo-1)*pageSize, pageSize);
	}
	
	/**
	 * 根据任务挂接ID获取流程事件总数
	 * @param id
	 * @return
	 */
	@Override
	public Long getCountByWorkflowTaskId(int id){
		String hql = "from WorkflowEvents w where w.workflowTaskFunMount.id = " + id;
		return workflowEventsDaoUtil.getCount(hql);
	}
	
	@Override
	public void execEventMethod(WorkflowEvents taskEvent, BaseEntity entity) {
		// TODO Auto-generated method stub
		String beanName = taskEvent.getServiceId();
		Object bean = SpringContextUtil.getBean(beanName);
		if(!EntityUtils.isNotNull(bean)){
			logger.info("获取bean实例失败。");
			return;
		}
		String methodName = taskEvent.getServiceMethod();
		String strParams = taskEvent.getMethodParams();
		if(StringUtils.isNotEmpty(strParams)){
		String[] params=strParams.split(",");		
		String[] values=new String[params.length];
			for(int i=0;i<params.length;i++){
				String proValue=EntityUtils.getPropertyValue(entity, params[i]);
				values[i]=new String(proValue);
			}
			EntityUtils.invokeMethod(bean, methodName, values);
		}else{
				EntityUtils.invokeMethod(bean, methodName, null);			
		}
	}

}
